﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;

namespace Web.Page
{
    public partial class WLKC_Edit : BasePage
    {
        static string ysl;//这里非常关键啊！
        static string yzxdj;
        
        KC_Dao dao = new KC_Dao();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {             
                initValue();
               
            }
           
        }
        public static double PJDJ=0;
        private void initValue()
        {
            string id = Request.QueryString["id"];
            this.hdId.Value = id;
            
            //
            KC_Dao dao = new KC_Dao();
            KC kc = dao.Query(id);
            InitWL();
            if (this.ddlWLLX.Items.FindByValue(kc.WPID.ToString()) != null)
                this.ddlWLLX.Items.FindByValue(kc.WPID.ToString()).Selected = true;
            ZL_GYS_Dao daoGYS = new ZL_GYS_Dao();
            this.ddlGYS.DataSource = daoGYS.QueryAll();
            this.ddlGYS.DataValueField = "ID";
            this.ddlGYS.DataTextField = "MC";
            this.ddlGYS.DataBind();
            this.ddlGYS.Items.Insert(0, new ListItem("请选择", "0"));
            ZL_WL_Dao daoWL = new ZL_WL_Dao();
            ZL_WL wl = daoWL.Query(kc.WPID.ToString());
            if (wl != null)
            {
                if (this.ddlGYS.Items.FindByValue(wl.GYSID.ToString()) != null)
                    this.ddlGYS.Items.FindByValue(wl.GYSID.ToString()).Selected = true;
            }
            PJDJ = Convert.ToDouble(kc.PJDJ);
            // 仓位
            ZL_CW_Dao daoCW = new ZL_CW_Dao();
            List<ZL_CW> lsCW = daoCW.QueryAll();
            this.ddlCWID.DataSource = lsCW;
            this.ddlCWID.DataValueField = "ID";
            this.ddlCWID.DataTextField = "BH";
            this.ddlCWID.DataBind();
            this.ddlCWID.Items.Insert(0, new ListItem("请选择", "0"));
            if (this.ddlCWID.Items.FindByValue(kc.CWID.ToString()) != null)
                this.ddlCWID.Items.FindByValue(kc.CWID.ToString()).Selected = true;
            
            // 仓库
            ZL_CK_Dao daoCK = new ZL_CK_Dao();
            List<ZL_CK> lsCK = daoCK.QueryAll();
            this.ddlCKID.DataSource = lsCK;
            this.ddlCKID.DataValueField = "ID";
            this.ddlCKID.DataTextField = "MC";
            this.ddlCKID.DataBind();
            this.ddlCKID.Items.Insert(0, new ListItem("请选择", "0"));
            if (this.ddlCKID.Items.FindByValue(kc.CKID.ToString()) != null)
                this.ddlCKID.Items.FindByValue(kc.CKID.ToString()).Selected = true;

            // 单位
            CS_JBDW_Dao daoDW = new CS_JBDW_Dao();
            List<CS_JBDW> lsDw = daoDW.Query4WL();
            this.ddlDW.DataSource = lsDw;
            this.ddlDW.DataValueField = "BH";
            this.ddlDW.DataTextField = "MC";
            this.ddlDW.DataBind();
            this.ddlDW.Items.Insert(0, new ListItem("请选择", "0"));
            ListItem li = this.ddlDW.Items.FindByValue(kc.DW);
            if(li != null)
                li.Selected = true;

            this.tbSL.Text = kc.SL.ToString();
            this.tbZXDJ.Text = kc.ZXDJ.ToString();
            ysl = this.tbSL.Text.Trim();
            yzxdj = this.tbZXDJ.Text.Trim();
        }


        /// <summary>
        /// 清空所有TextBox内容
        /// </summary>
        private void ClearControlsValue()
        {
            this.lbResult.Text = "";
        }
      

        private void InitWL()
        {
            this.ddlWLLX.Items.Clear();
            ZL_WL_Dao daoWL = new ZL_WL_Dao();
            List<ZL_WL> lsZP = daoWL.QueryAll();
            this.ddlWLLX.DataSource = lsZP;
            this.ddlWLLX.DataValueField = "ID";
            this.ddlWLLX.DataTextField = "MC";
            this.ddlWLLX.DataBind();
            this.ddlWLLX.Items.Insert(0, new ListItem("请选择", "0"));
        }

        /// <summary>
        /// 保存按钮---吴泽浩
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave0_Click(object sender, ImageClickEventArgs e)
        {
            this.lbResult.Text = "";
            if (this.ddlWLLX.SelectedValue == "0")
            {
                this.lbResult.Text = "请选择物料类型";
                return;
            }
            else
            {
                KC_Dao dao = new KC_Dao();
                KC kc = dao.Query(this.hdId.Value);
                if (kc.WPID.ToString() != this.ddlWL.SelectedValue)
                {
                    if (dao.IsCreateKC(this.ddlWL.SelectedValue))
                    {
                        this.lbResult.Text = "此物料已存在，不能重复建仓";
                        return;
                    }
                }
            }
            if (this.ddlCKID.SelectedValue == "0")
            {
                this.lbResult.Text = "请选择仓库";
                return;
            }
            if (this.ddlCWID.SelectedValue == "0")
            {
                this.lbResult.Text = "请选择仓位";
                return;
            }

            KC entity = new KC()
            {
                ID = Guid.Parse(this.hdId.Value),
                WPID = Guid.Parse(this.ddlWL.SelectedValue),
                WPLX = "01",
                CKID = Guid.Parse(this.ddlCKID.SelectedValue),
                CWID = Guid.Parse(this.ddlCWID.SelectedValue),
                SL = int.Parse(this.tbSL.Text.Trim()),
                ZXDJ = float.Parse(this.tbZXDJ.Text.Trim()),
                DW = this.ddlDW.SelectedValue,
                PJDJ = Convert.ToDouble((int.Parse(this.tbSL.Text) * int.Parse(this.tbZXDJ.Text) + (int.Parse(ysl) * int.Parse(yzxdj))) / Convert.ToDouble(int.Parse(this.tbSL.Text) + int.Parse(ysl)))
            };

            string resultMessage = "";
            try
            {
                KC_Dao dao = new KC_Dao();
                int result = dao.Update(entity);

                if (result == 1)
                {
                    resultMessage = "保存成功";
                   // ClearControlsValue();
                }
            }
            catch (Exception ex)
            {
                resultMessage = "保存失败" + ex.Message;
            }
            finally
            {
                this.lbResult.Text = resultMessage;
            }
        }

        protected void btnClear_Click(object sender, ImageClickEventArgs e)
        {
           
            ClearControlsValue();
            initValue();
            
        }

        protected void btnReturn_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("~/Page/WLKC_Query.aspx");
        }

        protected void ddlGYS_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindWl();
        }

        protected void ddlWLLX_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindWl();
        }

        private void BindWl()
        {
            this.ddlWL.Items.Clear();
            ZL_WL_Dao daoWL = new ZL_WL_Dao();
            List<ZL_WL> listWL = daoWL.QueryBySql("", "", ddlWLLX.SelectedValue, ddlGYS.SelectedValue);
            this.ddlWL.DataSource = listWL;
            this.ddlWL.DataTextField = "MC";
            this.ddlWL.DataValueField = "ID";
            this.ddlWL.DataBind();
            this.ddlWL.Items.Insert(0, new ListItem("请选择", "0"));
        }

        protected void ddlWL_SelectedIndexChanged(object sender, EventArgs e)
        {
            ZL_WL_Dao daoWL = new ZL_WL_Dao();
            ZL_WL wl = daoWL.Query(ddlWL.SelectedValue);
            tbGG.Text = wl.GG;
            if (ddlWL.SelectedIndex == 0)
                tbGG.Text = "";
        }
    }
}